<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
	var tables = $('#dataTables').DataTable({
		"sAjaxSource" : "{{ urlfor "InspectsController.Index"}}"
		,"bLengthChange": false
		,"fnServerData" : function(sSource, aoData, fnCallback) {
			var params = checkQueryTableData(aoData)
			$('#search_key').val() ? params.filter = $('#search_key').val() : ''
			$.post(sSource, JSON.stringify(params),function(resp) {
				fnCallback(resp);
			},'json')
		}
		,columns: [
			{"mDataProp" : "Id", "bSortable": false}
            ,{"data" : "Number", "bSortable": false}
			,{"data" : "ProductName", "bSortable": false}
            ,{"data" : "Unit", "bSortable": false}
			,{"data" : "Pinyin", "bSortable": false}
            ,{"data" : "UnitPrice", "bSortable": false}
			,{"data" : "CreatedAt", "bSortable": false, render:function(data, type, full, meta) {
				return full.CreatedBy +'/'+timetrans(data);
			}}
			,{"data" : "UpdatedAt", "bSortable": false, render:function(data, type, full, meta) {
				return full.UpdatedBy +'/'+timetrans(data);
			}}
			 ,{   
				"mDataProp": "Action", 
				"bSortable": false, 
				"sWidth":"10%",
				"render": function ( data, type, full, meta ) { 
					btns = '<a class="am-btn am-btn-secondary am-btn-xs" href="javascript:;" onclick="return editInspects('+ full.Id +')">编辑</a>';
                    btns += '&nbsp;<a class="am-btn am-btn-warning am-btn-xs" href="javascript:;" onclick="return editPrice('+ full.Id +', \''+ full.ProductName +'\')">调价</a>';
					if(full.IsStop == 1) {
						btns += '&nbsp;<a href="javascript:;" class="am-btn am-btn-default am-btn-xs" onclick="delInspects('+ full.Id +');return false;">启用</a>';
					} else {
						btns += '&nbsp;<a href="javascript:;" class="am-btn am-btn-danger am-btn-xs" onclick="delInspects('+ full.Id +');return false;">停用</a>';
					}
					return btns;  
				}  
			 } 
		]
		,"aLengthMenu":[20, 50, 100, 200]
		,'bLengthChange': true
		,"serverSide": true
		,"bPaginate" : true
		,"bFilter" : false	//是否启动过滤、搜索功能 
		,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
		,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
		  
	});
	
	$("#searchsubmit").bind("click", function () { //点击按钮 触发table重新请求服务器
		$("#dataTables").dataTable().fnDraw(false);
	});
	
	function delInspects(id) {
		layer.confirm('确定要操作所选记录吗？', function(index){
			$.post('{{ urlfor "InspectsController.Delete"}}', {inspects_id:id}, function(res){
				if(res.code == 1) {
					layer.msg(res.msg)
				} else {
					tables.ajax.reload();
				}
				layer.close(index);
			}, 'json');
		});
	}
	
	function dialogReback() {
		tables.ajax.reload();
	}
    function editPrice(id, name) {
        var url = '{{ urlfor "InspectsController.EditPrice"}}' + '/' + id;
        layer.open({
            type: 2,
            title: name+' 项目调价',
            shadeClose: false,
            shade: 0.2,
            maxmin: false,
            shift: 1,
            area: ['900px', '700px'],
            content: url
        });
        return false;
    }
	function editInspects(Id) {
        var title = '添加项目';
        if(Id >0){
            title = '编辑项目';
        }
        var url = '{{ urlfor "InspectsController.Edit"}}' + '/' + Id;
        layer.open({
            type: 2,
            title: title,
            shadeClose: false,
            shade: 0.2,
            maxmin: false,
            shift: 1,
            area: ['680px', '320px'],
            content: url
        });
		return false;
    }
	
</script>